package com.copro.chemdb.constants;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import org.ihelpuoo.db.sql.SQLSegment;

import com.copro.simple.Application;
import com.copro.simple.SimpleException;
import com.copro.simple.db.QueryCallback;

public class ConstTypeUtils {

	static class ListGetter implements QueryCallback {

		private List<ConstType> list;

		public ListGetter(List<ConstType> list) {
			this.list = list;
		}

		public List<ConstType> getList() {
			return this.list;
		}

		@Override
		public void exec(ResultSet rs) throws SQLException {
			while (rs.next()) {
				list.add(new ConstType(rs));
			}
		}
	}

	/**
	 * Provide the query function for one object
	 * 
	 * @author zozoh
	 * 
	 */
	static class DetailGetter implements QueryCallback {

		private ConstType constType;

		public DetailGetter(ConstType constType) {
			this.constType = constType;
		}

		public ConstType getObject() {
			return this.constType;
		}

		@Override
		public void exec(ResultSet rs) throws SQLException {
			if (rs.first())
				this.constType = new ConstType(rs);

		}

	}

	public static List<ConstType> getConstTypeAll(Application app)
			throws SimpleException {
		ListGetter callback = new ListGetter(new LinkedList<ConstType>());
		SQLSegment sql = app.getSQL("getConstTypeAll");
		app.query(sql.toString(), callback);
		return callback.getList();
	}

	public static ConstType getConstTypeById(Application app, int id)
			throws SimpleException {
		SQLSegment sql = app.getSQL("getConstType");
		sql.set("id", id <= 0 ? "NULL" : id);
		DetailGetter callback = new DetailGetter(new ConstType());
		app.query(sql.toString(), callback);
		return callback.getObject();
	}
}
